package com.hw.job_1;

public class Student implements java.io.Serializable {

    private static final long serialVersionUID = -5814716593800822421L;

    // 学号、姓名、年龄
    private String name;
    private String id;
    private int age;

    public Student() {
    }

    public Student(String name, String id, int age) throws Exception {
        setName(name);
        setId(id);
        setAge(age);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) { this.name = name; }

    public String getId() {
        return id;
    }

    public void setId(String id) throws IdException {
        // 学号需满足4位整数
        if (id.matches("\\d{4}")) {
            this.id = id;
        } else {
            // 抛出学号异常
            throw new IdException("学号异常，需满足4位整数的格式");
        }
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) throws AgeException {
        if (age >= 0 && age <150) {
            this.age = age;
        } else {
            // 抛出年龄异常
            throw new AgeException("年龄异常，需要大于等于0岁并且小于150岁");
        }
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", id='" + id + '\'' +
                ", age=" + age +
                '}';
    }

    // 通过姓名和id来确认是否为同一个学生
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o;

        if (!name.equals(student.name)) return false;
        return id.equals(student.id);
    }

    @Override
    public int hashCode() {
        int result = name.hashCode();
        result = 31 * result + id.hashCode();
        return result;
    }
}
